package com.zy.explore.db.infrastructure.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zy.explore.db.infrastructure.plugins.metafill.FieldMetaObjectHandler;
import com.zy.explore.db.infrastructure.plugins.sqltransform.CustomInnerInterceptor;
import com.zy.explore.db.infrastructure.plugins.sqltransform.CustomMybatisInterceptor;
import com.zy.explore.db.infrastructure.util.PropertiesUtil;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * <p>建类说明：数据源配置类</p>
 *
 * @author 中国印
 * @date 2022年02月17日 15:37:58
 */
@MapperScan(value = "com.zy.explore.db.mybatis.domain.dao", sqlSessionTemplateRef = "sqlTransformSqlSessionTemplate", sqlSessionFactoryRef = "sqlTransformSqlSessionFactory")
@Configuration
@ConditionalOnProperty(DataSourceConfig.PREFIX)
public class DataSourceConfig {

    public static final String PREFIX = "spring.datasource";

    @Bean
    @ConfigurationProperties(prefix = PREFIX)
    public DataSource dataSource(){
        return  new DruidDataSource();
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") @Autowired DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(PropertiesUtil.getProperty("mybatis-plus.mapper-locations", "classpath:/com/zy/explore/db/domain/mybatis/mapper/*.xml")));

        // MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        MybatisPlusInterceptor interceptor = new CustomMybatisInterceptor();
        interceptor.addInnerInterceptor(new CustomInnerInterceptor());

        // 添加sql转换拦截器
        sqlSessionFactoryBean.setPlugins(interceptor);

        GlobalConfig globalConfig = GlobalConfigUtils.defaults();
        // mybatis-plus全局配置设置元数据对象处理器为自己实现的那个
        globalConfig.setMetaObjectHandler(new FieldMetaObjectHandler());
        sqlSessionFactoryBean.setGlobalConfig(globalConfig);

        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        mybatisConfiguration.setMapUnderscoreToCamelCase(true);
        // 设置日志打印实现类(不设置则不输出)
        if (Boolean.parseBoolean(PropertiesUtil.getProperty("spring.datasource.custom.showSql", "true"))) {
            mybatisConfiguration.setLogImpl(StdOutImpl.class);
        }
        sqlSessionFactoryBean.setConfiguration(mybatisConfiguration);
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") @Autowired SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager(@Qualifier("dataSource") @Autowired DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}
